﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Utilities
{
    public class CookieHelper
    {
        public static void ClearCookie(string key)
        {
            key = Utility.MD5Encrypt(key);
            var cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                cookie.Value = String.Empty;
                cookie.Expires = DateTime.Now.AddYears(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        public static void SetCookieValue(string key, string value)
        {
            key = Utility.MD5Encrypt(key);
            var cookie = HttpContext.Current.Request.Cookies[key] ?? new HttpCookie(key);
            cookie.Value = Utility.Encrypt(value);
            cookie.Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        //public static void SetCookieTable(string key, string[] keyvalue, string[] value)
        //{
        //    key = Utility.Encrypt(key);
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
        //    if (cookie == null)
        //    {
        //        cookie = new HttpCookie(key);
        //    }
        //    for (int i = 0; i < keyvalue.Length; i++)
        //    {
        //        keyvalue[i] = Utility.Encrypt(keyvalue[i]);
        //        cookie[keyvalue[i]] = Utility.Encrypt_Process(value[i], true);
        //        //cookie.Values[keyvalue[i]] = Utility.Encrypt_Process(value[i], true);
        //    }
        //    cookie.Expires = DateTime.Now.AddDays(1);
        //    HttpContext.Current.Response.Cookies.Add(cookie);
        //}

        public static string GetCookieValue(string key)
        {
            key = Utility.MD5Encrypt(key);
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie == null)
                return String.Empty;
            return Utility.Decrypt(cookie.Value);
        }

        //public static string GetCookieTableValue(string key, string keyvalue)
        //{
        //    key = Utility.Encrypt(key);
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
        //    if (cookie == null)
        //        return String.Empty;
        //    keyvalue = Utility.Encrypt(keyvalue);
        //    return Utility.Decrypt_Process(cookie.Values[keyvalue], true);
        //}
    }
}
